const app = getApp();
Page({
  data: {
    is_login:false,
    index:0,
    type_list:[{
      id:1,
      type_name:'支出'
    },{
      id:2,
      type_name:'收入'
    }],
    date:'',
    bill_info:[],
    consume_type:1,
    con_index:1,
    consume_list:[],
    consume_id:'',
    trans_money:'',
  },

  onLoad:function(options){
    let _this = this,
      bill_id = options.bill_id;
    _this.setData({
      bill_id:bill_id
    })
  },

  onShow:function(){
    this.getConsumeList(); //获取种类信息
  },

  //获取种类
  getConsumeList:function(){
    let _this = this,
      consume_type = _this.data.consume_type;
    app._post('api/member/getConsumeList', {consume_type:consume_type}, function(result) {
      _this.setData({
        consume_list:result.data
      })
      _this.getUserInfo(); //获取用户信息
    }); 
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code != 200){
        wx.navigateTo({
          url: '/pages/auth/auth'
        })
      }else{
        _this.getBillInfo();
      }
    });
  },

  //获取账单相信
  getBillInfo:function(){
    let _this = this,
      bill_id = _this.data.bill_id,
      consume_list = _this.data.consume_list,
      con_index = _this.data.con_index;
    app._post('api/member/get_bill_info', {bill_id:bill_id}, function(result) {
      //获取种类
      for(var i=0;i<consume_list.length;i++){
        if(consume_list[i].consume_id == result.data.consume_id){
          con_index = i
        }
      }

      _this.setData({
        bill_info:result.data,
        date:result.data.trans_day,
        consume_id:result.data.consume_id,
        trans_money:result.data.trans_money,
        con_index:con_index
      })
    });
  },

  //提交
  change_bill_sub:function(){
    let _this = this;
    app._post('api/member/edit_bill', {
      consume_id:_this.data.consume_id,
      trans_money:_this.data.trans_money,
      trans_day:_this.data.date,
      trans_type:_this.data.consume_type,
      bill_id:_this.data.bill_id,
    }, function(result) {
      if(result.code == 200){
        app.showSuccess(result.msg);
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      }
    });
  },

  //输入金额
  input_money:function(e){
    let _this = this,
     trans_money = e.detail.value;
    _this.setData({
      trans_money:trans_money
    })
  },

  //日期选择器
  bindDateChange:function(e){
    let _this = this;
    _this.setData({
      date: e.detail.value
    })
  },

  //删除账本
  del_bill:function(){
    let _this = this,
     bill_id = _this.data.bill_id;
    wx.showModal({
      title: '温馨提示',
      content: '确认删除该记录吗？',
      success (res) {
        if (res.confirm) {
          app._post('api/member/del_bill', {bill_id:bill_id}, function(result) {
            if(result.code == 200){
              app.showSuccess(result.msg);
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //收支变更
  typeChange:function(e){
    let _this = this,
    type_list = _this.data.type_list,
    consume_type = _this.data.consume_type,
    select_id = e.detail.value;
    consume_type = type_list[select_id].id;
    _this.setData({
      consume_type:consume_type,
      index:select_id,
      con_index:0
    })
    app._post('api/member/getConsumeList', {consume_type:consume_type}, function(result) {
      _this.setData({
        consume_list:result.data,
        consume_id:result.data[0].consume_id
      })
    }); 
  },

  //收支种类
  consumeChange:function(e){
    let _this = this,
     consume_list = _this.data.consume_list;
     _this.setData({
      con_index:e.detail.value,
      consume_id:consume_list[e.detail.value].consume_id
     })
  },
})